import Vue from "vue";
import VueRouter from "vue-router";
import Layout from "../layout/index.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    component: Layout,
    redirect: "/home",
    children: [
      {
        path: "/home",
        name: "Home",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/views-home/Home.vue"
          ),
        meta: { icon: "success", title: "用户管理" },
      },
      {
        path: "/category",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/views-about/index.vue"
          ),
        meta: { icon: "success", title: "商品" },
        children: [
          {
            path: "/about",
            name: "About",
            component: () => import("../views/views-about/About.vue"),
            meta: { icon: "success", title: "品类管理" },
          },
          {
            path: "/list",
            name: "List",
            component: () => import("../views/views-about/List.vue"),
            meta: { icon: "error", title: "列表" },
          },
        ],
      },
    ],
  },
  {
    path: "/404",
    component: () => import("@/views/error-page/404"),
    hidden: true,
  },
  { path: "*", redirect: "/404", hidden: true },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

const auth = {
  isLogin() {
    return true;
  },
};
// 全局守卫 验证登录
router.beforeEach((to, from, next) => {
  if (to.path == "/home") {
    if (auth.isLogin()) {
      next();
    } else {
      next("/login");
    }
  } else {
    next();
  }
});

export default router;
